<template>
  <el-select-tree
    v-model="selectedVal"
    :data="options"
    :placeholder="placeholder"
    :props="{ children: 'children', label: 'name'}"
    @selected="selectedInfo" />
</template>
<script>
import ElSelectTree from '@/components/tree/ElSelectTree'
export default {
  components: { ElSelectTree },
  props: {
    value: { type: [String, Array], default() { return '' } },
    placeholder: { type: String, default() { return '请选择' } },
    code: { type: String, required: true }
  },
  data() {
    return {
      selectedVal: '',
      resultVal: '',
      options: []
    }
  },
  watch: {
    value: function(val) {
      this.analysisValue()
    },
    resultVal: function(val) {
      this.$emit('input', val)
    }
  },
  mounted() {
    this.initData()
  },
  created() {
    this.analysisValue()
  },
  methods: {
    analysisValue() {
      if (this.value) {
        if (this.value.split(':').length === 2) {
          this.selectedVal = this.value.split(':')[0]
        } else {
          this.selectedVal = this.value
        }
      } else {
        this.selectedVal = ''
      }
      console.log(this.selectedVal)
    },
    async initData() {
      try {
        const response = await this.$store.dispatch('upmsCategoryDictTreeByType', this.code)
        console.log(response)
        this.options = response
      } catch (_) {
        console.log()
      }
      // this.treeData = response
    },
    selectedInfo(result) {
      this.resultVal = result.id + ':' + result.name
    }
  }
}
</script>
<style lang="scss">
</style>
